$('#addSickPage').on('hidden.bs.modal', function () {
    $("#pDeptCode").val('');
    $("#cDeptCode").val('');
    $("#sickName").val('');
});

$('#addColumnDeptPage').on('hidden.bs.modal', function () {
    $("#_pDeptCode").val('');
    $("#deptName").val('');
});

$(function () {
    if (editType != 'info') {
        regValidate(["#_columnDataForm"]);

        $('input[type=checkbox][name="columnDept"][idd="oneDept"]').change(function () {
            var value = $(this).val();
            var checked =  $(this).prop("checked");
            if (checked) {
                $('input[type=checkbox][name="columnDept"][idd="'+value+'"]').each(function () {
                    this.checked = true;
                });
            } else {
                $('input[type=checkbox][name="columnDept"][idd="'+value+'"]').each(function () {
                    this.checked = false;
                });
            }
        });
    }
});

function doChangeGrade() {
    var grade = $('#grade').val();
    if (grade == 1) {
        $('#_parentCodeDiv').css('display', 'none');
        $('#_sicknessDiv').css('display', 'none');
        $('#parentCode').val('');
        $('#sicknessNames').html('');
        $('#sicknessCodes').val('');
    } else {
        $('#_parentCodeDiv').css('display', 'block');
        $('#_sicknessDiv').css('display', 'block');
    }
}

function doSave() {
    if(!$("#_columnDataForm").valid()) return;//校验不通过
    if(!checkColumnData()) return;
    var columnName = $('#columnName').val();
    var grade = $('#grade').val();
    var sortNum = $('#sortNum').val();
    var parentCode = $('#parentCode').val();
    var sicknessCodes = getLabelNoteStr('_columnSickTags', false);
    var columnDeptCodes = getLabelNoteStr('_columnDeptTags', false);
    var remark = $('#remark').val();
    sortNum = sortNum ? sortNum : 999;
    var postData = {
        columnName : columnName,
        grade : grade,
        parentCode : parentCode,
        sortNum : sortNum,
        remark : remark,
        sicknessCodes : sicknessCodes == 'all' ? '' : sicknessCodes,
        columnDeptCodes : columnDeptCodes == 'all' ? '' : columnDeptCodes,
    };
    var url = basePath + 'output/addColumn';
    if (editType == 'edit') {
        postData.id = oneColumnInfo.id;
        postData.columnCode = oneColumnInfo.columnCode;
        postData.status = oneColumnInfo.status;
        url = basePath + 'output/editColumn';
    } else {
        postData.outputId = searchInfo.outputId;
    }
    $.ajax({
        type: 'post',
        url: url,
        dataType: 'json',
        data: JSON.parse(JSON.stringify(postData)),
        success : function (data) {
            if (!data) {
                toastr.error(common_save_fail);
                return;
            }
            if (data.result) {
                doBack();
                return;
            }
            if (!data.result && data.code == 'EXIST') {
                toastr.warning('此栏目名已存在');
                return;
            }
            if (!data.result) {
                toastr.error(data.errorDesc);
                return;
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
            return;
        }
    });
}

function string2List(str) {
    if (!str || str == '') {
        return [];
    }
    return str.split(',');
}

function checkColumnData() {
    var grade = $('#grade').val();
    var parentCode = $('#parentCode').val();
    var sicknessCodes = getLabelNoteStr('_columnSickTags', false);
    var columnDeptCodes = getLabelNoteStr('_columnDeptTags', false);
    if (!columnDeptCodes || columnDeptCodes == '') {
        toastr.error('科室信息不能为空');
        return false;
    }
    if (grade == 2) {
        if (!parentCode || parentCode == '') {
            toastr.error('父级栏目不能为空');
            return false;
        }
        if (!sicknessCodes || sicknessCodes == '') {
            toastr.error('疾病信息不能为空');
            return false;
        }
    }
    return true;
}

function doCheckDataChange() {
    var sortNum = $('#sortNum').val();
    var remark = $('#remark').val();
    var columnName = $('#columnName').val();
    var grade = $('#grade').val();
    var parentCode = $('#parentCode').val();
    var sicknessCodes = getLabelNoteStr('_columnSickTags', false);
    var columnDeptCodes = getLabelNoteStr('_columnDeptTags', false);

    var checkData = {
        sortNum : sortNum,
        remark : remark,
        columnName : columnName,
        sortNum : sortNum,
        grade : grade,
        remark : remark,
        columnDeptCodes : columnDeptCodes,
    };
    if (grade == 2) {
        checkData.parentCode = parentCode;
        checkData.sicknessCodes = sicknessCodes;
    }
    if (editType == 'add') {
        oneColumnInfo.grade = 1;
    }
    if (editType != 'info' && !Compare(checkData, oneColumnInfo)) {
        swalClosePageToastr(doBack);
        return;
    }
    doBack();
}

function doBack() {
    var str = searchStr.replace(/,/g, '&');
    window.location.href = basePath + 'output/outputPage?t=' + new Date().getTime() + '&' + str;
}

function openAddSickPage() {
    doSickSearch();
    $('#addSickPage').modal('show');
}

function doPDeptChangeEvent() {
    var pDeptCode = $('#pDeptCode').val();
    $('#cDeptCode').val('');
    var strHtml = '<option value="">--请选择--</option>';
    if (!pDeptCode || pDeptCode == '') {
        $('#cDeptCode').html(strHtml);
    } else {
        var postData = {
            parentCode : pDeptCode,
        };
        $.ajax({
            type: 'post',
            url: basePath + 'output/selectDept?t=' + new Date().getTime(),
            dataType: 'json',
            data: postData,
            success : function (data) {
                if(data && data.result){
                    setDeptSelect(data.content);
                }else if(data&&!data.result){
                    toastr.error(data.message);
                }else{
                    toastr.error('获取科室信息失败');
                }
            },
            error: function (request) {
                delayToastr(common_system_error);
            }
        });
    }
}

function setDeptSelect(list) {
    var htmlStr = '<option value="">--请选择--</option>';
    if (list) {
        for (var i=0; i<list.length; i++) {
            htmlStr += '<option value="'+list[i].code+'">'+list[i].name+'</option>';
        }
    }
    $('#cDeptCode').html(htmlStr);
    doSickSearch();
}

function doSickSearch() {
    var pDeptCode = $('#pDeptCode').val();
    var cDeptCode = $('#cDeptCode').val();
    var sickName = $('#sickName').val();
    var sicknessCodes = getLabelNoteStr('_columnSickTags', false);
    $('#sickTable').bootstrapTable('refresh', {
        url: basePath + 'output/pageFindSickData?t=' + new Date().getTime(),
        query: {
            pDeptCode : pDeptCode,
            cDeptCode : cDeptCode,
            sicknessName : sickName,
            sicknessCodes : sicknessCodes
        },
    });
}

function getDeptParams(params) {
    var pDeptCode = $('#pDeptCode').val();
    var cDeptCode = $('#cDeptCode').val();
    var sickName = $('#sickName').val();
    var sicknessCodes = getLabelNoteStr('_columnSickTags', false);
    return {
        pDeptCode : pDeptCode,
        cDeptCode : cDeptCode,
        sicknessName : sickName,
        sicknessCodes : sicknessCodes,
        pageNo : params.pageNumber,
        pageSize : params.pageSize
    };
}

function doAddSick() {
    var datas = getSelectedRows('#sickTable');
    if(!datas || datas.length < 1){
        toastr.error('请先选择要添加的疾病');
        return;
    }
    var tagList = [];
    for (var i in datas) {
        var sick = {
            value : datas[i].sicknessCode,
            text : datas[i].sicknessName,
        };
        tagList.push(sick);
    }
    $('#addSickPage').modal('hide');
    var sicknessCodes = getLabelNoteStr('_columnSickTags', false);
    if (sicknessCodes == 'all') {
        htmlLableByList(tagList, "_columnSickTags");
    } else {
        addLableByList(tagList, "_columnSickTags");
    }
}

function openAddColumnDeptPage() {
    /*var columnDeptCodes = getLabelNoteStr('_columnDeptTags', false);
    if (columnDeptCodes == 'all') {
    } else {
        var columnDeptList = columnDeptCodes.split(',');
        $.each(columnDeptList,function(n,value) {
            $('input[type=checkbox][name="columnDept"][value="'+value+'"]').prop('checked',true);
        });
    }*/
    doColumnDeptSearch();
    $('#addColumnDeptPage').modal('show');
}

function doColumnDeptSearch() {
    var pDeptCode = $('#_pDeptCode').val();
    var deptName = $('#deptName').val();
    var columnDeptCodes = getLabelNoteStr('_columnDeptTags', false);
    $('#deptTable').bootstrapTable('refresh', {
        url: basePath + 'output/pageFindColumnDeptData?t=' + new Date().getTime(),
        query: {
            parentCode : pDeptCode,
            deptName : deptName,
            columnDeptCodes : columnDeptCodes
        },
    });
}

function getDeptInfoParams(params) {
    var pDeptCode = $('#_pDeptCode').val();
    var deptName = $('#deptName').val();
    var columnDeptCodes = getLabelNoteStr('_columnDeptTags', false);
    return {
        parentCode : pDeptCode,
        deptName : deptName,
        columnDeptCodes : columnDeptCodes,
        pageNo : params.pageNumber,
        pageSize : params.pageSize
    };
}

function doAddColumnDept() {
    var datas = getSelectedRows('#deptTable');
    if(!datas || datas.length < 1){
        toastr.error('请选择指定科室');
        return;
    }
    var tagList = [];
    for (var i in datas) {
        var sick = {
            value : datas[i].deptCode,
            text : datas[i].deptName,
        };
        tagList.push(sick);
    }
    $('#addColumnDeptPage').modal('hide');
    var columnDeptCodes = getLabelNoteStr('_columnDeptTags', false);
    if (columnDeptCodes == 'all') {
        htmlLableByList(tagList, "_columnDeptTags");
    } else {
        addLableByList(tagList, "_columnDeptTags");
    }
}

function queryColumnDept() {
    var name = $.trim($("#columnDeptName").val());
    if(!name){
        $("#depts label").show();
        return false;
    }
    //全部隐藏
    $("#depts label").hide();
    $("label[idd*="+ name +"]").show();
}

function clearSick() {
    var tagList = [{
        value : 'all',
        text : '全部'
    }];
    htmlLableByList(tagList, "_columnSickTags");
}

function clearColumnDept() {
    var tagList = [{
        value : 'all',
        text : '全部'
    }];
    htmlLableByList(tagList, "_columnDeptTags");
}


